<?php

class HTML_Form extends HTML  {
	private $attributeAction;
	private $attributeMethod;

	/**
	* Finals used for attributes of button
	*/
	const HTML_FORM = 'form';
	const METHOD_GET = 'GET';
	const METHOD_POST = 'POST';
	const ATTRIBUTE_ACTION = 'action';
	const ATTRIBUTE_METHOD = 'method';
	
	public function __construct($parent, $properties)  {
		PropertySetter::setProperties($this, $properties);
		$parent->addChildElement($this);
	}
	
	public function setAttributeAction ($action)  {
		$this->attributeAction = $action;
	}
	
	public function setAttributeMethod ($method)  {
		if (in_array($method, $this->getMethods()))  {
			$this->attributeMethod = $method;
		}
	}
	
	public function getMethods ()  {
		$methods = array();
		$methods[] = self::METHOD_GET;
		$methods[] = self::METHOD_POST;
		
		return $methods;
	}
	
	public function getAttributeAction ()  {
		return $this->attributeAction;
	}
	
	public function getAttributeMethod ()  {
		return $this->attributeMethod;
	}
	
	public function getFormAttributes ()  {
	
		$output = '';
	
		if (!empty($this->attributeAction))  {
			$output .= ' '. self::ATTRIBUTE_ACTION. '="'. $this->getAttributeAction(). '"';
		}
		if (!empty($this->attributeMethod))  {
			$output .= ' '. self::ATTRIBUTE_METHOD. '="'. $this->getAttributeMethod(). '"';
		}
	
		if ($output == '')  {
			$output = ' ';
		}
	
		return $output;
	}
	
	public static function getAttributes ()  {
		$attributes = array();
		$parentAttributes = parent::getAttributes();
		$attributes[] = self::ATTRIBUTE_ACTION;
		$attributes[] = self::ATTRIBUTE_METHOD;
		return array_merge($parentAttributes, $attributes);
	}
	
	/**
	* This function is used to return the complete button with all attributes
	*/
	public function toString ()  {
		$output =	'<'. self::HTML_FORM.
		$this->getHTMLAttributes().
		$this->getFormAttributes(). '>';
	
		if ($this->getChildElements())  {
			foreach ($this->getChildElements() as $element)  {
				$output .= $element->toString();
			}
		}
	
		$output .= '</'. self::HTML_FORM. '>';
	
		return $output;
	}
	
}